/**
  * @(#) GenericHibernateSupport.java  
  *
  * @author lihg 
  * @version V1.0 2013-10-5  
  *
  * Copyright (C) 2010,2013 , Lihg
  */

/**
 * 
 */
package cn.com.dshome.acquisition.dao.support;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import cn.com.dshome.acquisition.dao.HibernateDao;

/**
 * 
 * Purpose:
 * 
 * @author lihg 
 * @see	    
 * @since
 */
@Repository("hibernateDao")
public class GenericHibernateSupport implements HibernateDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
	/* (non-Javadoc)
	 * @see cn.com.dshome.acquisition.dao.HibernateDao#save(java.lang.Object)
	 */
	@Override
	public <T> Serializable save(T entity) {
		return getSession().save(entity);
	}

	/* (non-Javadoc)
	 * @see cn.com.dshome.acquisition.dao.HibernateDao#update(java.lang.Object)
	 */
	@Override
	public <T> void update(T entity) {
		getSession().update(entity) ;
	}

	/* (non-Javadoc)
	 * @see cn.com.dshome.acquisition.dao.HibernateDao#listAll(java.lang.Class)
	 */
	@Override
	public <T> List<T> listAll(Class<T> clazz) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz) ;
		return listByDetachedCriteria(detachedCriteria);
	}

	/* (non-Javadoc)
	 * @see cn.com.dshome.acquisition.dao.HibernateDao#listByDetachedCriteria(org.hibernate.criterion.DetachedCriteria)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> listByDetachedCriteria(DetachedCriteria detachedCriteria) {
		final Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
		return criteria.list();
	}

	/* (non-Javadoc)
	 * @see cn.com.dshome.acquisition.dao.HibernateDao#listByHQL(java.lang.String, java.util.Map<java.lang.String,java.lang.Object>[])
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> listByHQL(String hql, Map<String, Object> params) {
		final Query query = getSession().createQuery(hql);
		setParameters(query, params);
		return query.list() ;
	}

	/* (non-Javadoc)
	 * @see cn.com.dshome.acquisition.dao.HibernateDao#get(java.lang.Class, java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T> T get(Class<T> clazz, Serializable id) {
		return (T) getSession().get(clazz,id);
	}
	
	/**
	 * 获取Session
	 * 
	 * @author lihg
	 * @version V1.0 2013-8-8
	 * @return
	 */
	private Session getSession() {
		Session session = sessionFactory.getCurrentSession();
		if (null == session) {
			session = sessionFactory.openSession();
		}
		return session;
	}
	
	/**
	 * 设置参数
	 * 
	 * @author lihg
	 * @version V1.0 2013-8-8
	 * @param query
	 * @param params
	 */
	private void setParameters(final Query query, final Map<String, Object> params) {
		if (null != params) {
			final Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();
			while (iterator.hasNext()) {
				final Entry<String, Object> entry = iterator.next();
				query.setParameter(entry.getKey(), entry.getValue());
			}
		}
	}
}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * 
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2013-10-5 下午01:24:58 lihg 创建版本
 */